package com.admin.service.ipl;

import com.admin.mapper.AdChannelMapper;
import com.apis.admin.AdChannelControllerApi;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.DeleteBatchByIds;
import com.baomidou.mybatisplus.core.injector.methods.DeleteById;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.model.admin.dtos.ChannelDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang.StringUtils;
import org.jsoup.helper.StringUtil;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdChannelService extends ServiceImpl<AdChannelMapper, AdChannel> implements com.admin.service.AdChannelService {

    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {
//       判断 channelDto是否为null
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
//        检查分页参数
        dto.checkParam();
//        根据条件来进行查询
        Page page= new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        /*使用qw中方法判断name是否为空*/
        lambdaQueryWrapper.like(dto.getName()!=null,AdChannel::getName,dto.getName());
        /*使用自定义方法判断name是否为空*/
//        if (StringUtils.isNotBlank(dto.getName())){
//            lambdaQueryWrapper.like(AdChannel::getName,dto.getName());
//        }
        IPage r=page(page,lambdaQueryWrapper);
        //        结果封装  向上转级
        ResponseResult responseResult=new PageResponseResult(dto.getPage(),dto.getSize(),(int)r.getTotal());
//      将数据存储到分页的方法中 进行返回
       responseResult.setData(r.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult del(Integer id) {
//      根据id查询这条信息
        AdChannel byId = getById(id);
//        判断状态是否可以删除
        if (byId.getStatus()==true){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
//        删除 返回状态码
        removeById(byId.getId());
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult saveAdChannel(AdChannel adChannel) {
//     判断dto是否为空
        if (adChannel==null){
//            返回不成功的状态信息
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
//        添加创建时间
        adChannel.setCreatedTime(new Date());
//        调用添加方法完成添加 返回状态码
        save(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult update(AdChannel adChannel) {
        //     判断dto是否为空
        if (adChannel==null){
//            返回不成功的状态信息
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
//        调用修改方法完成添加 返回状态码
        updateById(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
